package com.bsu.ascm.controls;

import java.util.List;

import android.R;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ListViewTableAdapter extends BaseAdapter {
	private Context context;
	private List<TableRow> table;
	public ListViewTableAdapter(Context context,List<TableRow> table){
		this.context = context;
		this.table = table;
	}
	@Override
	public int getCount() {
		return table.size();
	}

	@Override
	public Object getItem(int position) {
		return table.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TableRow tableRow = table.get(position);
		if(position==0)
			return new TableRowView(context,tableRow,TableRowView.ROW_TITLE);
		else if(position%2==1)
			return new TableRowView(context,tableRow,TableRowView.ROW_OOD_NUMBER);
		else if(position%2==0)
			return new TableRowView(context,tableRow,TableRowView.ROW_EVEN_NUMBER);
		else
			return new TableRowView(this.context,tableRow,TableRowView.ROW_NONE);
	}
	
	 /** 
     * TableRowView 实现表格行的样式 
     * @author hellogv 
     */  
    class TableRowView extends LinearLayout {
    	public static final int ROW_NONE = 0;												//默认行样式
    	public static final int ROW_TITLE = 1;												//标题行样式
    	public static final int ROW_OOD_NUMBER = 2;											//奇数行样式
    	public static final int ROW_EVEN_NUMBER = 3;										//偶数行样式
    	private int rowType = ROW_NONE;														//当前行样式
        public TableRowView(Context context, TableRow tableRow ,int rt) {  
            super(context);  
            rowType = rt;
            this.setOrientation(LinearLayout.HORIZONTAL);  
            for (int i = 0; i < tableRow.getSize(); i++) {									//逐个格单元添加到行  
                TableCell tableCell = tableRow.getCellValue(i);  
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(  
                        tableCell.width-1, tableCell.height-1);								//按照格单元指定的大小设置空间  
                layoutParams.setMargins(1, 1, 1, 1);										//预留空隙制造边框  
                if (tableCell.type == TableCell.STRING) {									//如果格单元是文本内容  
                    TextView textCell = new TextView(context);  
                    textCell.setLines(1);  
                    textCell.setGravity(Gravity.CENTER);
                    //设置背景颜色，0为标题行设置灰色#6e6e6e，数据行奇数行#4D4D4D,偶数行#4D4D4D
                    if(rowType==ROW_TITLE){
                    	textCell.setTextAppearance(context, R.style.TextAppearance_Large);
                    	textCell.setTextColor(Color.rgb(0, 0, 0));
                    	textCell.setBackgroundColor(Color.rgb(110, 110, 110));
                    }
                    else if(rowType==ROW_OOD_NUMBER){
                    	textCell.setTextAppearance(context, R.style.TextAppearance_Medium);
                    	textCell.setTextColor(Color.rgb(255, 255, 255));
                    	textCell.setBackgroundColor(Color.rgb(77, 77, 77));
                    }
                    else if(rowType==ROW_EVEN_NUMBER){
                    	textCell.setTextAppearance(context, R.style.TextAppearance_Medium);
                    	textCell.setTextColor(Color.rgb(255, 255, 255));
                    	textCell.setBackgroundColor(Color.rgb(77, 77, 77));
                    }
                    else
                    	textCell.setBackgroundColor(Color.BLACK);//背景黑色  
                    textCell.setText(String.valueOf(tableCell.value));  
                    addView(textCell, layoutParams);  
                } else if (tableCell.type == TableCell.IMAGE) {//如果格单元是图像内容  
                    ImageView imgCell = new ImageView(context);  
                    imgCell.setBackgroundColor(Color.BLACK);//背景黑色  
                    imgCell.setImageResource((Integer) tableCell.value);  
                    addView(imgCell, layoutParams);  
                }  
            }  
            //this.setBackgroundColor(Color.WHITE);//背景白色，利用空隙来实现边框
            this.setBackgroundColor(Color.rgb(77, 77, 77));
        }  
    }  
    /** 
     * TableRow 实现表格的行 
     * @author hellogv 
     */  
    static public class TableRow {  
        private TableCell[] cell;  
        public TableRow(TableCell[] cell) {  
            this.cell = cell;  
        }  
        public int getSize() {  
            return cell.length;  
        }  
        public TableCell getCellValue(int index) {  
            if (index >= cell.length)  
                return null;  
            return cell[index];  
        }  
    }  
    /** 
     * TableCell 实现表格的格单元 
     * @author hellogv 
     */  
    static public class TableCell {  
        static public final int STRING = 0;  
        static public final int IMAGE = 1;  
        public Object value;
        public int width;  
        public int height;  
        private int type;  

	    public TableCell(Object value, int width, int height, int type) {  
		      this.value = value;  
		      this.width = width;  
		      this.height = height;  
		      this.type = type;  
	    }
    }  
}
